import torch
from torch import conv2d, nn

c1 = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=0, bias=False)
a = torch.randn(1,1,10,10)
b = c1(a)

c1_sd = c1.state_dict() 
c1_w = c1_sd['weight']
w_scale = torch.max(torch.abs(torch.max(c1_w)), torch.abs(torch.min(c1_w))) / 127.0

c1_w_q = torch.round(c1_w / w_scale)
c1_w_q = c1_w_q.clamp(-128, 127)



a_scale = torch.max(torch.abs(torch.max(a)), torch.abs(torch.min(a))) / 127.0
a_q = torch.round(a / a_scale)
a_q = a_q.clamp(-128, 127)

c1_sd['weight'] = c1_w_q
c1.load_state_dict(c1_sd)
b_q = c1(a_q).int()

print(b)
print(b_q*a_scale*w_scale)
# print((torch.abs(b_q*a_scale*w_scale) - torch.abs(b)) / torch.abs(b))